//
//  MyItemsController.swift
//  Fancyswap
//
//  Created by Jonathan Muschalle on 2015-04-12.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit

class MyItemsController: UITableViewController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        FancyTools.authentication(self)
        var refreshControl = UIRefreshControl()
        refreshControl.addTarget(self, action: Selector("refreshData"), forControlEvents: UIControlEvents.ValueChanged)
        self.refreshControl = refreshControl
        refreshData()
    }
    
    override func viewDidAppear(animated: Bool) {
        FancyTools.authentication(self)
    }
    
    func refreshData() {
        myItems.loadItems(self)
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let currentCell = tableView.dequeueReusableCellWithIdentifier("MyItemCell", forIndexPath: indexPath) as MyItemCell
        return myItems.getCellByIndex(indexPath, cell: currentCell)
    }
    
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myItems.getTotalItems()
    }

    @IBAction func home(sender: AnyObject) {
//        self.dismissViewControllerAnimated(false, completion: nil)
        var storyboard = UIStoryboard(name: "Main", bundle: nil)
        var controller = storyboard.instantiateViewControllerWithIdentifier("main") as UIViewController
        self.presentViewController(controller, animated: true, completion: nil)
    }
    
    

}
